package com.example.psysystemhomework.ui.home;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.example.psysystemhomework.LoginActivity;
import com.example.psysystemhomework.R;
import com.example.psysystemhomework.UploadActivity;
import com.example.psysystemhomework.adapter.HomeAdapter;
import com.example.psysystemhomework.bean.BackBean;
import com.example.psysystemhomework.bean.RBean;
import com.example.psysystemhomework.databinding.FragmentHomeBinding;
import com.example.psysystemhomework.util.AppManager;
import com.example.psysystemhomework.util.ImageUtil;
import com.example.psysystemhomework.util.NetUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Random;

public class HomeFragment extends Fragment {
    private HomeViewModel homeViewModel;
    private FragmentHomeBinding binding;
    private Handler handler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.arg1) {
                case 1:
                    adapter.setJson(jsonObject);
                    adapter.notifyDataSetChanged();
                    break;
                case 2:
                    List<BackBean> list = (List<BackBean>) msg.obj;
                    Random random = new Random();
                    ImageUtil imageUtil = new ImageUtil();
                    Drawable bitmap = imageUtil.loadImageFromNetwork(list.get(random.nextInt(list.size())).getImageUrl());
                    lineBack.setBackground(bitmap);
                    break;
                case 3:
                    break;
                default:
            }

            super.handleMessage(msg);
        }
    };
    private JSONObject jsonObject;
    private HomeAdapter adapter;
    private CheckBox itsMe;
    private ListView listView;
    private ImageView imageUpload;
    private RelativeLayout lineBack;
    private List<BackBean> o1;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        listView = binding.listStoryShow;
        lineBack = binding.lineBack;
        itsMe = binding.itsMe;
        imageUpload = binding.imageUpload;
        setOnClik();
        jsonObject = new JSONObject();
        adapter = new HomeAdapter(jsonObject, getContext());
        listView.setAdapter(adapter);
        getJOSnData();

        return root;
    }

    private void setOnClik() {
        itsMe.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    adapter.setShowMe();
                } else {
                    getJOSnData();
                }
            }
        });
        imageUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppManager appManager = AppManager.getAppManager();
                appManager.addActivity(new UploadActivity());
                Intent intent = new Intent(getContext(), appManager.currentActivity().getClass());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        getJOSnData();
        super.onResume();
    }

    private void getJOSnData() {
        new Thread() {
            @Override
            public void run() {
                NetUtil netUtil = new NetUtil(getContext());
                String getAllStory = netUtil.getGetAllStory(1, 100, "");

                try {
                    jsonObject = new JSONObject(getAllStory);
                    Log.i("jsonObject", "run: " + jsonObject.toString());

                    Message message = new Message();
                    message.arg1 = 1;
                    handler.sendMessage(message);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                super.run();
            }
        }.start();


//        new Thread() {
//            @Override
//            public void run() {
//                NetUtil netUtil = new NetUtil();
//                JSONObject getAllBack = netUtil.getAllBack();
//                Gson gson = new Gson();
//                RBean backBeans = gson.fromJson(getAllBack.toString(), RBean.class);
//                o1 = gson.fromJson(backBeans.getData().toString(), new TypeToken<List<BackBean>>() {
//                }.getType());
//                Message message = new Message();
//                message.arg1 = 2;
//                message.obj = o1;
//                handler.sendMessage(message);
//
//
//                super.run();
//            }
//        }.start();


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}